package ar.com.sne.model.composer.error;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.api.Grid;

import foundation.common.util.BusinessException;
import foundation.common.util.CommonWebUtil;
import foundation.common.util.ExceptionsKeyParameters;


@Service
@Scope("prototype")
public class BusinessExceptionComposer extends GenericForwardComposer {

	private Grid errorList;
	//private Textbox stacktrace;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		BusinessException exception = (BusinessException) requestScope
				.get("javax.servlet.error.exception");
		Rows rows = new Rows();

		if (exception.getExceptionsKeys() != null) {
			for (String error : exception.getExceptionsKeys()) {
				Row row = new Row();
				Label label = new Label();
				Hbox hbox = new Hbox();
				Image img = new Image();
				img.setSrc("~./foundation-web/common/imagenes/punto-rojo.png");
				img.setWidth("8px");
				img.setHeight("8px");
				img.setAlign("center");
				String etiqueta = CommonWebUtil.getProperty(error);

				if (etiqueta != null && etiqueta.length() > 0)
					label.setValue(etiqueta);
				else
					label.setValue(error);

				hbox.appendChild(img);
				hbox.appendChild(label);
				row.appendChild(hbox);
				rows.appendChild(row);
			}
		}
		if (exception.getExceptionsKeysParameters() != null) {
			for (ExceptionsKeyParameters error : exception.getExceptionsKeysParameters()) {
				Row row = new Row();
				Label label = new Label();
				Hbox hbox = new Hbox();
				Image img = new Image();
				img.setSrc("~./foundation-web/common/imagenes/punto-rojo.png");
				img.setWidth("8px");
				img.setHeight("8px");
				img.setAlign("center");
				String etiqueta = CommonWebUtil.getProperty(error.getKey(),error.getParameters());

				if (etiqueta != null && etiqueta.length() > 0)
					label.setValue(etiqueta);
				else
					label.setValue(error.getKey());

				hbox.appendChild(img);
				hbox.appendChild(label);
				row.appendChild(hbox);
				rows.appendChild(row);
			}
		}
		if (exception.getDescriptions() != null) {
			for (String error : exception.getDescriptions()) {
				Row row = new Row();
				Label label = new Label();
				Hbox hbox = new Hbox();
				Image img = new Image();
				img.setSrc("~./foundation-web/common/imagenes/itemRojo.png");
				img.setWidth("8px");
				img.setHeight("8px");
				img.setAlign("center");
				String etiqueta = error;

				if (etiqueta != null && etiqueta.length() > 0)
					label.setValue(etiqueta);
				else
					label.setValue(error);

				hbox.appendChild(img);
				hbox.appendChild(label);
				row.appendChild(hbox);
				rows.appendChild(row);
			}
		}

		errorList.appendChild(rows);
		final Writer result = new StringWriter();
		final PrintWriter printWriter = new PrintWriter(result);
		exception.printStackTrace(printWriter);

		// this.stacktrace=(Textbox)comp.getFellow("stacktrace");
		//stacktrace.setValue(result.toString());
	}

}
